﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Linq;
using Watcher.Data;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
using Watcher.Logic;
using Apache.NMS.ActiveMQ;
using Apache.NMS;

namespace Watcher
{
    public partial class Servers : Form
    {
        private string topicName = null;
        private IConnectionFactory connectionFactory;
        private IConnection connection;
        private ISession session;
        private IMessageProducer producer;
        public Servers()
        {
            InitializeComponent();
            ServerCollection servers = null;
            string path = "serverlistsource.xml";
            XmlRootAttribute xRoot = new XmlRootAttribute();
            xRoot.ElementName = "servercollection";

            XmlSerializer serializer = new XmlSerializer(typeof(ServerCollection), xRoot);

            StreamReader reader = new StreamReader(path);
            try
            {
                servers = (ServerCollection)serializer.Deserialize(reader);
            }
            catch (Exception e)
            {
                Console.WriteLine("Error: " + e);
            }
            reader.Close();
            List<Server> sl = new List<Server>();
            foreach (Server s in servers._server)
            {
                sl.Add(s);
            }
            dataGridView1.SelectionMode = DataGridViewSelectionMode.FullRowSelect;
            
            dataGridView1.DataSource = sl;
        }

        private void DataGridView1_SelectionChanged(object sender, EventArgs e)
        {
            // Update the labels to reflect changes to the selection.
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            int i = dataGridView1.CurrentRow.Index;
            int count = dataGridView1.Rows[i].Cells.Count;
            string name = dataGridView1.Rows[i].Cells[0].Value.ToString();
            string ip = dataGridView1.Rows[i].Cells[count - (count - 1)].Value.ToString();
            string port = dataGridView1.Rows[i].Cells[count - (count - 2)].Value.ToString();
            string Default = dataGridView1.Rows[i].Cells[count - (count - 3)].Value.ToString();

            Server _server = new Server();
            _server.Default = Default;
            _server.Name = name;
            _server.Ip = ip;
            _server.Port = port;

            Globals _globals = Globals.getInstance();
            _globals.MessageQueueServer = _server;

            

            this.Close();
        }

        private void tconnection_Click(object sender, EventArgs e)
        {

            int i = dataGridView1.CurrentRow.Index;
            int count = dataGridView1.Rows[i].Cells.Count;
            string name = dataGridView1.Rows[i].Cells[0].Value.ToString();
            string ip = dataGridView1.Rows[i].Cells[count - (count - 1)].Value.ToString();
            string port = dataGridView1.Rows[i].Cells[count - (count - 2)].Value.ToString();
            string Default = dataGridView1.Rows[i].Cells[count - (count - 3)].Value.ToString();

            bool ConnectionSuccess = false;
            string brokerUri = "tcp://" + ip + ":" + port;
            
            topicName = "simulation";

           
            try
            {
                connectionFactory = new ConnectionFactory(brokerUri);
                this.connection = this.connectionFactory.CreateConnection();
                ConnectionStatus.Text = "Connection Established";
                ConnectionStatus.ForeColor = Color.Green;
            }
            catch (Exception Ex)
            {
                ConnectionStatus.Text = "Connection could not be established.";
                ConnectionStatus.ForeColor = Color.Red;
                Console.Write(Ex.ToString());
            }


        }
    }
}
